package com.kk.base.service.home;

import com.kk.base.bean.PageBean;
import com.kk.base.dao.home.HomeUserAddressDao;
import com.kk.base.entity.home.HomeUser;
import com.kk.base.entity.home.HomeUserAddress;
import com.kk.base.entity.home.HomeUserTypeEnum;
import com.kk.base.util.SessionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @info :  前台用户地址地址业务处理层
 * @author: wangzihan
 * @date : 2021/1/25 14:21
 */
@Service
public class HomeUserAddressService {

    @Autowired
    private HomeUserAddressDao homeUserAddressDao;

    /**
     * 根据前台用户地址id查询
     *
     * @param id
     * @return
     */
    public HomeUserAddress find(Long id) {
        return homeUserAddressDao.find(id);
    }

    /**
     * 按照前台用户地址名查找前台用户地址
     *
     * @param name
     * @return
     */
    public HomeUserAddress findByName(String name) {
        return homeUserAddressDao.findByName(name);
    }


    /**
     * 查询当前用户拥有默认地址
     *
     * @return
     */
    public HomeUserAddress findByIsDefaultAndHomeUserTypeAndHomeUser_Id(Integer isEcode,HomeUserTypeEnum HomeUserType,Long homeUserId) {
        return homeUserAddressDao.findByIsDefaultAndHomeUserTypeAndHomeUser_Id(isEcode,HomeUserType,homeUserId);
    }


    /**
     * 按手机号查找
     *
     * @param mobile
     * @return
     */
    public HomeUserAddress findByMobile(String mobile) {
        return homeUserAddressDao.findByMobile(mobile);
    }


    /**
     * 前台用户地址添加/编辑操作
     *
     * @param homeUserAddress
     * @return
     */
    public HomeUserAddress save(HomeUserAddress homeUserAddress) {
        return homeUserAddressDao.save(homeUserAddress);
    }

    public PageBean<HomeUserAddress> findList(PageBean<HomeUserAddress> pageBean,String search,String searchCategory) {

        Specification<HomeUserAddress> specification = new Specification<HomeUserAddress>() {
            @Override
            public Predicate toPredicate(Root<HomeUserAddress> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                HomeUser homeUser = SessionUtil.getHomeUser();

                List<Predicate> predicates = new ArrayList<>();

                if (homeUser != null ) {
                    Predicate predicate = criteriaBuilder.equal(root.get("homeUser"), homeUser);
                    predicates.add(predicate);
                }

                if (StringUtils.isNotBlank(searchCategory)){
                    if (StringUtils.isNotBlank(search)) {
                        if ("name".equals(searchCategory)){
                            predicates.add(criteriaBuilder.like(root.get("name"), "%" + search + "%"));
                        } else if ("mobile".equals(searchCategory)){
                            predicates.add(criteriaBuilder.like(root.get("mobile"), "%" + search + "%"));
                        }
                    }
                }

                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Pageable pageable = PageRequest.of(pageBean.getCurrentPage() - 1, pageBean.getPageSize());
        Page<HomeUserAddress> findAll = homeUserAddressDao.findAll(specification, pageable);
        pageBean.setContent(findAll.getContent());
        pageBean.setTotal(findAll.getTotalElements());
        pageBean.setTotalPage(findAll.getTotalPages());
        return pageBean;
    }


    /**
     * 按照前台用户地址id删除
     *
     * @param id
     */
    public void delete(Long id) {
        homeUserAddressDao.deleteById(id);
    }

    /**
     * 返回前台用户地址总数
     *
     * @return
     */
    public long total() {
        return homeUserAddressDao.count();
    }


    /**
     * 通过当前登录用户和类型查询
     * @param homeUserType
     * @param userId
     * @return
     */
   public List<HomeUserAddress> findByHomeUserTypeAndHomeUser_Id(HomeUserTypeEnum homeUserType,Long userId){
       return homeUserAddressDao.findByHomeUserTypeAndHomeUser_Id(homeUserType,userId);
   }


}
